package com.woniu.common.entity;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@ApiModel("统一响应对象")
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    // 响应状态码最好在一个地方统一定义好，不要在程序的每个位置自己定义，很难维护
    @ApiModelProperty("响应状态码")
    private Integer code;
    @ApiModelProperty("响应消息")
    private String message;
    @ApiModelProperty("响应数据")
    private T data;

    private static <T> Result<T> common(int code, String message, T data) {
        return new Result<>(code, message, data);
    }

    public static <T> Result<T> commonByRow(int row) {
        if (row > 0) {
            return ok();
        } else {
            return error();
        }
    }

    public static <T> Result<T> commonByFlag(Boolean flag) {
        if (flag) {
            return ok();
        } else {
            return error();
        }
    }

    public static <T> Result<T> ok() {
        return common(com.woniu.common.entity.ResultCode.SUCCESS.getCode(), com.woniu.common.entity.ResultCode.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> ok(T data) {
        return common(com.woniu.common.entity.ResultCode.SUCCESS.getCode(), com.woniu.common.entity.ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> ok(int code, String message, T data) {
        return common(com.woniu.common.entity.ResultCode.SUCCESS.getCode(), com.woniu.common.entity.ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> error() {
        return common(com.woniu.common.entity.ResultCode.ERROR.getCode(), com.woniu.common.entity.ResultCode.ERROR.getMessage(), null);
    }

    public static <T> Result<T> error(int code) {
        return common(code, com.woniu.common.entity.ResultCode.ERROR.getMessage(), null);
    }

    public static <T> Result<T> error(T data) {
        return common(com.woniu.common.entity.ResultCode.ERROR.getCode(), com.woniu.common.entity.ResultCode.ERROR.getMessage(), data);
    }

    public static <T> Result<T> error(int code, String message) {
        return common(code, message, null);
    }

    public static <T> Result<T> error(int code, String message, T data) {
        return common(code, message, data);
    }
}
